package cn.com.dcsgo.controller;

import cn.com.dcsgo.domain.MemberAddr;
import cn.com.dcsgo.dto.MemberAddrAddDTO;
import cn.com.dcsgo.dto.MemberAddrUpdateDTO;
import cn.com.dcsgo.model.Result;
import cn.com.dcsgo.service.MemberAddrService;
import cn.com.dcsgo.utils.AuthUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员收货地址相关接口
 *
 * @author Dcsgo
 * @since 2025/10/31 16:19
 */
@RestController
@RequestMapping("p/address")
@Tag(name = "会员收货地址相关接口")
public class AddressController {
    @Resource
    private MemberAddrService memberAddrService;

    @GetMapping("list")
    @Operation(summary = "获取收货地址列表", description = "返回数据按是否为默认地址、创建时间降序排序")
    public Result<List<MemberAddr>> getAddrList() {
        return Result.success(memberAddrService.getListByOpenid(AuthUtil.getOpenid()));
    }

    @PutMapping("defaultAddr/{addrId}")
    @Operation(summary = "更改默认收货地址")
    public Result<Void> updateDefaultAddr(@PathVariable Long addrId) {
        return Result.handle(memberAddrService.updateDefaultAddr(AuthUtil.getOpenid(), addrId));
    }

    @PostMapping
    @Operation(summary = "添加收货地址")
    public Result<Void> addAddr(@Validated @RequestBody MemberAddrAddDTO addrAddDTO) {
        return Result.handle(memberAddrService.save(addrAddDTO, AuthUtil.getOpenid()));
    }

    @GetMapping("addrInfo/{addrId}")
    @Operation(summary = "获取收货地址信息")
    public Result<MemberAddr> getById(@PathVariable Long addrId) {
        return Result.success(memberAddrService.getById(addrId));
    }

    @PutMapping("updateAddr")
    @Operation(summary = "修改收货地址")
    public Result<Void> updateAddr(@Validated @RequestBody MemberAddrUpdateDTO updateDTO) {
        return Result.handle(memberAddrService.update(updateDTO, AuthUtil.getOpenid()));
    }

    @DeleteMapping("deleteAddr/{addrId}")
    @Operation(summary = "删除收货地址")
    public Result<Void> deleteAddr(@PathVariable Long addrId) {
        return Result.handle(memberAddrService.removeById(addrId,AuthUtil.getOpenid()));
    }

}
